import { get, post } from "@/utils/request";
import { getToken } from "@/utils/auth";
import config from "@/config/config.json";
import { responseHandle } from "@/utils/request/requestHandle";
import Toast from "@vant/weapp/dist/toast/toast";

/**
 * 文生图
 * @param data 图片描述，图片大小
 * @returns
 */
export function createImage(data) {
  return post({
    url: "/AI_Image/createImage",
    data,
  });
}

/**
 * 文生图
 * @param data 图片地址，图片风格
 * @returns
 */
export function imgAiImage(data) {
  return post({
    url: "/AI_Image/imgAiImage",
    data,
  });
}

/**
 * 获取绘图结果
 * @param requestId 绘图的ID，用于查询绘图结果
 * @returns
 */
export function getImageByRequestId(requestId) {
  return get({
    url: `/AI_Image/getImageByRequestId?requestId=${requestId}`,
  });
}

/**
 * 抠图
 * @param data imageFileUrl type
 * @returns
 */
export function getImgSegmentation(data) {
  return post({
    url: `/AI_Image/imgSegmentation`,
    data,
  });
}

/**
 *
 * @param data imageFileUrl:需要修复的图像文件地址 (type:修复类型  clean = 通用放大清晰化。face = 人像放大清晰化。)
 * @returns
 */
export function imgHDrepair(data) {
  return post({
    url: `/AI_Image/imgHDrepair`,
    data,
  });
}

/**
 * 上传图片
 * @param taskId 任务ID
 * @returns 创作状态
 */
export function resultImgUpload(data) {
  return post({
    url: "/AI_Image/resultImgUpload",
    data,
  });
}
/**
 * 获取文生图、抠图、高清、消除等操作的结果
 * @param taskId 任务ID
 * @returns 创作状态
 */
export function getImageByTaskId(taskId) {
  return get({
    url: `/AI_Image/getImageByTaskId?taskId=${taskId}`,
  });
}

/**
 * 获取AI绘图历史记录
 * @param page 页码
 * @returns AI绘图记录
 */
export function getAiImgRecordList({ type, page }) {
  return get({
    url: `/AI_Image/getAiImgRecordList?type=${type}&page=${page}`,
  });
}


/**
 * 获取AI绘图历史详情
 * @param recordId 记录id
 * @returns AI绘图记录详情信息
 * 
 */
export function getAiImgRecordInfo(recordId) {
  return get({
    url: `/AI_Image/getAiImgRecordInfo?recordId=${recordId}`,
  });
}



/**
 * 根据记录id删除绘图记录
 * @param recordIds 记录ID
 * @returns
 */
export function delAiImgRecord(recordIds) {
  return get({
    url: `/AI_Image/delAiImgRecord?recordIds=${recordIds}`,
  });
}
/**
 * @description 根据背景图类型id获取背景图列表
 * @param typeId 背景图类型id
 * @returns
 */
export function backgroundImageType() {
  return get({
    url: `/AI_Image/getBackgroundImgType?typeId=0`,
  });
}
/**
 * @description 根据背景图类型id获取背景图列表
 * @param typeId 背景图类型id
 * @returns
 */
export function backgroundImageList({ typeId = 0 }) {
  return get({
    url: `/AI_Image/getBackgroundImgList?typeId=${typeId}&isPtype=true`,
  });
}

/**
 * @description 抠图
 * @param Object 抠图对象
 * @params
 * @returns
 */
export function imgInpaint(data) {
  return post({
    url: `/AI_Image/imgInpaint`,
    data,
  });
}

/**
 * AI生成背景图
 * @param data
 * @returns
 */
export function imgAiBackground(data) {
  return post({
    url: `/AI_Image/imgAiBackground`,
    data,
  });
}

/**
 * AI- 指令改图
 * @param data
 * @returns
 */
export function imgByteedit(data) {
  return post({
    url: `/AI_Image/imgByteedit`,
    data,
  });
}

/**
 * AI生成背景图-v2
 * @param data
 * @returns
 */
export function imgAiBackgroundV2(data) {
  return post({
    url: `/AI_Image/imgAiBackground-V2`,
    data,
  });
}

/**
 * ai背景图上传
 * @param url
 * @param formData
 * @returns
 */

export async function aiBackgroundUpload(url, formData) {
  return new Promise(async (resolve, reject) => {
    const res = await uni.uploadFile({
      url: `${config.siteDomain}/api/AI_Image/aiBackgroundUpload`,
      filePath: url,
      name: 'refImgFile',
      header: {
        satoken: getToken(),
        "content-type": "multipart/form-data",
      },
      formData: formData,
    });
    res.data = JSON.parse(res.data);
    if (res.data.code == 200) {
      resolve(res.data);
    } else {
      console.log(res, 'dasdasdasddasd');

      Toast.fail(res.data.message);
      responseHandle(res);
      reject(res.data);
    }
  })
}

/**
 * 做任务获取算力值奖励
 * @returns
 */
export function doTaskGetReward() {
  return post({
    url: `/user/doTaskGetReward`,
  });
}

/**
 * 获取AI绘图的算力消耗记录
 * @returns
 */
export function getAiImgRecordConsumeList({ type, page }) {
  return get({
    url: `/AI_Image/getAiImgRecordConsumeList?type=${type}&page=${page}`,
  });
}

/**
 * 获取AI高级背景模板列表
 * @returns
 */
export function getBackgroundImgTemplates(id) {
  return get({
    url: `/AI_Image/getBackgroundImgTemplates?tpType=${id}`,
  });
}

// /**
//  * 图生图风格参考图上传
//  * @returns
//  */
// export async function imgToImageUploadRefImg(url, formData) {
//   return responseHandle(
//     await uni.uploadFile({
//       url: `${config.siteDomain}/api/AI_Image/imgToImageUploadRefImg`,
//       filePath: url,
//       name: "refImgFile",
//       header: {
//         satoken: getToken(),
//         "content-type": "multipart/form-data",
//       },
//       formData,
//     })
//   );
// }


/**
 * 图生图风格参考图上传
 * @returns
 */
export async function imgToImageUploadRefImg(url, formData) {
  return new Promise(async (resolve, reject) => {
    const res = await uni.uploadFile({
      url: `${config.siteDomain}/api/AI_Image/imgToImageUploadRefImg`,
      filePath: url,
      name: "refImgFile",
      header: {
        satoken: getToken(),
        "content-type": "multipart/form-data",
      },
      formData,
    });
    res.data = JSON.parse(res.data);
    if (res.data.code == 200) {
      resolve(res.data.data.refImgUrl);
    } else {
      Toast.fail(res.data.message);
      responseHandle(res);
      reject(res.data);
    }
  })
}




/**
 * AI扩图
 * @returns
 */
export function imgOutPainting(data) {
  return post({
    url: `/AI_Image/imgOutPainting`,
    data
  });
}




/**
 * AI局部重绘
 * @returns
 */
export function imgSynthsis(data) {
  return post({
    url: `/AI_Image/imgSynthsis`,
    data
  });
}



/**
 * 图片文件上传
 * @returns
 */
export function editImgUpload(data) {
  return new Promise(async (resolve, reject) => {
    if (data.orgImg.size > 7 * 1024 * 1024) {
      Toast.fail("图片大小不能超过7MB");
      return reject(new Error("图片大小不能超过7MB"));
    }

    const res = await uni.uploadFile({
      url: `${config.siteDomain}/api/AI_Image/editImgUpload`,
      filePath: data.orgImg,
      name: 'orgImg',
      header: {
        satoken: getToken(),
        "content-type": "multipart/form-data",
      },
      formData: data,
    });
    res.data = JSON.parse(res.data);
    if (res.data.code == 200) {
      resolve(res.data.data);
    } else {
      Toast.fail(res.data.message);
      responseHandle(res);
      reject(res.data);
    }
  })
}

/**
 * 蒙版层图片上传
 * @returns
 */
export function maskImgUpload(data) {
  return new Promise(async (resolve, reject) => {
    const res = await uni.uploadFile({
      url: `${config.siteDomain}/api/AI_Image/maskImgUpload`,
      filePath: data.maskImg,
      name: 'maskImg',
      header: {
        satoken: getToken(),
        "content-type": "multipart/form-data",
      },
      formData: data,
    });
    res.data = JSON.parse(res.data);
    if (res.data.code == 200) {
      resolve(res.data.data.maskImgUrl);
    } else {
      Toast.fail(res.data.message);
      responseHandle(res);
      reject(res.data);
    }
  })
}




/**
 * 图生文
 * @returns
 */
export function imgToTxt(data) {
  return post({
    url: `/AI_Image/imgToTxt`,
    data
  });
}




/**
 * 发型编辑
 * @returns
 */
export function hairStyleEdit(data) {
  return post({
    url: `/AI_Image_amusement/hairStyleEdit`,
    data
  });
}




/**
 * 表情编辑
 * @returns
 */
export function emotionPortrait(data) {
  return post({
    url: `/AI_Image_amusement/emotionPortrait`,
    data
  });
}



/**
 * 年龄变换
 * @returns
 */
export function allAgeGeneration(data) {
  return post({
    url: `/AI_Image_amusement/allAgeGeneration`,
    data
  });
}




/**
 * 老照片修复
 * @returns
 */
export function convertPhotoV2(data) {
  return post({
    url: `/AI_Image_amusement/convertPhotoV2`,
    data
  });
}



/**
 * 智能变美
 * @returns
 */
export function facePretty(data) {
  return post({
    url: `/AI_Image_amusement/facePretty`,
    data
  });
}

/**
 * 查询操作说明
 * @returns
 */
export function queryOperateDescByType(type) {
  return get({
    url: `/operateDesc/queryOperateDescByType?type=${type}`,
  });
}



/**
 * 鑫鑫爱学
 * @returns
 */
export function analysisQuest(data) {
  return post({
    url: `/AI_Image/analysisQuest`,
    data
  });
}


/**
 * AI视频
 * @returns
 */
export function aiGenerationVideo(data) {
  return post({
    url: `/AI_Image/aiGenerationVideo`,
    data
  });
}



